package com.oneday.nuo.base.service.impl;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oneday.nuo.base.common.CommonStatic;
import com.oneday.nuo.base.common.NuoUtil;
import com.oneday.nuo.base.config.BaseService;
import com.oneday.nuo.base.dao.NuoMenuDAO;
import com.oneday.nuo.base.dto.NuoMenuDTO;
import com.oneday.nuo.base.dto.TreeDTO;
import com.oneday.nuo.base.entity.NuoMenuEntity;
import com.oneday.nuo.base.exception.NuoException;
import com.oneday.nuo.base.service.NuoMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * (NuoMenu)表服务实现类
 *
 * @author liushuai
 * @since 2021-03-13 23:37:21
 */
@Service("nuoMenuService")
public class NuoMenuServiceImpl extends BaseService<NuoMenuDAO, NuoMenuEntity> implements NuoMenuService {

    @Autowired
    private NuoMenuDAO nuoMenuDao;


    @Override
    public void saveMenu(NuoMenuDTO nuoMenuDTO) {
        if(StrUtil.isBlank(nuoMenuDTO.getPid())){
            nuoMenuDTO.setPid("0");
        }
        if(ObjectUtil.isNull(nuoMenuDTO.getSortIndex())){
            nuoMenuDTO.setSortIndex(NuoUtil.DEFAULT_SORT);
        }
        NuoMenuEntity entity = Convert.convert(NuoMenuEntity.class, nuoMenuDTO);
        this.saveOrUpdate(entity);
    }

    @Override
    public List<Tree<String>> queryMenuAll() {
        List<NuoMenuEntity> lists = nuoMenuDao.selectList(new QueryWrapper<NuoMenuEntity>());
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<String>> treeNodes = TreeUtil.build(lists, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getPid());
                    tree.setWeight(treeNode.getSortIndex());
                    tree.setName(treeNode.getName());
                });
        return treeNodes;
    }

    @Override
    public List<NuoMenuEntity> queryMenuAllList() {
        LambdaQueryWrapper<NuoMenuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(NuoMenuEntity::getId,NuoMenuEntity::getName,NuoMenuEntity::getSortIndex,NuoMenuEntity::getPid);
        return nuoMenuDao.selectList(wrapper);
    }


    @Override
    public NuoMenuDTO queryMenuById(String id) {
        NuoMenuEntity nuoMenuEntity = nuoMenuDao.selectById(id);
        NuoMenuDTO dto = Convert.convert(NuoMenuDTO.class, nuoMenuEntity);
        if(CommonStatic.TOP_PARENT_ID.equals(nuoMenuEntity.getPid())){
            dto.setPidName("根节点");
        }else{
            nuoMenuEntity = nuoMenuDao.selectById(nuoMenuEntity.getPid());
            dto.setPidName(nuoMenuEntity.getName());
        }

        return dto;
    }

    @Override
    public void deleteMenuById(String id) {
        List<String> ids = nuoMenuDao.queryMenuIdByPid(id);
        nuoMenuDao.deleteBatchIds(ids);
    }

    @Override
    public List<NuoMenuEntity> queryRoute() {
        List<NuoMenuEntity> lists = nuoMenuDao.selectList(new QueryWrapper<NuoMenuEntity>());
        return lists;
    }

    @Override
    public Map<String,Object> queryMenuByUserId(Long userId) {
        Map<String,Object> dataMap = new HashMap<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("d.user_id",String.valueOf(userId));
        wrapper.groupBy("a.component_name");
        List<NuoMenuDTO> dtos =  nuoMenuDao.queryMenuByUserId(wrapper);
        List<Tree<String>> treeNodes = TreeUtil.build(dtos, "0",  new TreeNodeConfig(),
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getPid());
                    tree.setWeight(treeNode.getSortIndex());
                    tree.setName(treeNode.getName());
                    tree.putExtra("componentName",treeNode.getComponentName());
                    tree.putExtra("path",treeNode.getPath());

                });
        dataMap.put("router",dtos);
        dataMap.put("menus",treeNodes);
        return dataMap;
    }


}
